package com.kuang.controller;


import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
 
    //查询全部书籍
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "books/allBook";
    }

    @RequestMapping("/toAddBook")
    //增加书籍
    public String toAddPaper(){
        return "books/addBook";
    }

    @RequestMapping("/addBook")
    //添加书籍的请求
    public String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdate")
    //修改页面
    public String toUpdatePaper(int id,Model model){
        Books books=bookService.queryBookById(id);
        model.addAttribute("Qbook",books);
        System.out.println(books);
        return "books/updateBook";
    }

    @RequestMapping("/updateBook")
    //修改书籍请求
    public String updateBook(Books books){
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/deleteBook/{bookId}")
    //删除书籍
    public String deleteBook(@PathVariable("bookId") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/querBook")
    //使用书籍编号查询书籍
    public String querBook(String queryBookNo,Model model){
        List<Books> books = bookService.queryBookByNo(queryBookNo);
        List<Books> list = new ArrayList<Books>();
        list.addAll(books);
        if(books==null){
            list = bookService.queryAllBook();
            model.addAttribute("error","未查到");
        }
        model.addAttribute("list",list);
        return "books/allBook";
    }

    @RequestMapping("/back")
    public String back(){
        return "index/index";
    }
}
